package edu.chalmers.sdx;

import edu.chalmers.targine.Mesh;
import edu.dhbw.andar.util.Model;

public class LevelPart extends Mesh{
//	private Model model;
	private float[][] heightMap;
	
	private int direction;
	protected final static int DIRECTION_FORWARD = 0;
	protected final static int DIRECTION_LEFT = 1;
	protected final static int DIRECTION_RIGHT = 2;
	
	public LevelPart(){
		model = null;
		type = SkidroidXtreme.SLOPE;
		heightMap = heightMap0;
	}
	
	public LevelPart(Model model, String type, float posX, float posY){
		this.model = new Model(model, type);
		this.model.setXpos(posX);
		this.model.setYrot(posY);
		this.type = SkidroidXtreme.SLOPE;
//		direction = DIRECTION_FORWARD;
		if(type.equals("ski_slope_2")){
			heightMap = heightMap1;
			direction = DIRECTION_FORWARD;
		} else if(type.equals("ski_slope_1")){
			heightMap = heightMap2;
			direction = DIRECTION_FORWARD;
		} else if(type.equals("ski_slope_3")){
			heightMap = heightMap3;
			direction = DIRECTION_FORWARD;
		} else if(type.equals("ski_slope_right_turn")){
			heightMap = heightMap4;
			direction = DIRECTION_RIGHT;
		} else if(type.equals("ski_slope_left_turn")){
			heightMap = heightMap5;
			direction = DIRECTION_LEFT;
		} else{
			heightMap = heightMap0;
			direction = DIRECTION_FORWARD;
		}
	}
	
	@Override
	public void update(){
		
	}
	
	public float heightAt(float x, float y){
		int xi = Math.round((x - model.xpos + 40f) / 8f);
		int yi = Math.round((y - model.ypos + 40f) / 8f);
//		Log.d("height", "checking coords " + xi + "," + yi);
		if(xi >= 0 && xi < 10 && yi >= 0 && yi < 10){
			return heightMap[xi][yi];
		}
		return -1f;
	}
	
	@Override
	public void finalize() {
		model.finalize();
	}
	
	public boolean isFinalized(){
		return model.STATE == Model.STATE_FINALIZED;
	}
	
	public String getType(){
		return model.name;
	}
	
	public Model getModel(){
		return model;
	}
	
	public boolean hasModel(){
		return model != null;
	}
	
	public void setDirection(int newDirection){
		direction = newDirection;
	}
	
	public int getDirection(){
		return direction;
	}
	
	public float getXPos(){
		return model.xpos;
	}
	
	public float getYPos(){
		return model.ypos;
	}
	
	//Default
	private float[][] heightMap0 = {
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0}
	};
	
	//Guppeli-gupp
	private float[][] heightMap1 = {
		{1,1,1,1,1,1,1,1,1,1},
		{3,3,3,3,3,3,3,3,3,3},
		{5,5,5,5,5,5,5,5,5,5},
		{3,3,3,3,3,3,3,3,3,3},
		{1,1,1,1,1,1,1,1,1,1},
		{1,1,1,1,1,1,1,1,1,1},
		{2,2,2,2,2,2,2,2,2,2},
		{3,3,3,3,3,3,3,3,3,3},
		{2,2,2,2,2,2,2,2,2,2},
		{1,1,1,1,1,1,1,1,1,1}
	};
	
	//CP-hopp
	private float[][] heightMap2 = {
		{1,1,1,1,1,1,1,1,1,1},
		{2,2,2,2,2,2,2,2,2,2},
		{5,5,5,5,5,5,5,5,5,5},
		{7,7,7,7,7,7,7,7,7,7},
		{7,7,7,7,7,7,7,7,7,7},
		{6,6,6,6,6,6,6,6,6,6},
		{5,5,5,5,5,5,5,5,5,5},
		{3,3,3,3,3,3,3,3,3,3},
		{2,2,2,2,2,2,2,2,2,2},
		{1,1,1,1,1,1,1,1,1,1}
	};
	
	//Puckelpist
	private float[][] heightMap3 = {
		{1,1,1,1,1,1,1,2,2,1},
		{1,2,2,1,1,1,1,2,3,1},
		{1,3,2,1,1,1,1,2,2,1},
		{1,2,2,1,2,3,2,2,1,1},
		{1,1,1,2,2,3,2,1,2,1},
		{1,1,1,1,1,1,1,1,1,1},
		{1,1,1,3,2,1,2,3,2,1},
		{2,2,2,2,2,1,2,2,2,1},
		{2,3,2,1,1,1,1,1,2,1},
		{2,2,2,1,1,2,2,1,1,1}
	};
	
	//Right turn
	private float[][] heightMap4 = {
		{10,9,8,7,6,5,4,3,2,1},
		{9,8,7,6,5,5,4,3,2,1},
		{8,7,6,6,5,5,4,3,2,1},
		{7,6,5,5,4,4,3,3,2,1},
		{6,5,5,4,4,3,3,2,2,1},
		{5,4,4,4,3,3,3,2,2,1},
		{4,4,3,3,3,3,2,2,2,1},
		{3,3,3,2,2,2,2,2,2,1},
		{2,2,2,2,2,2,2,2,1,1},
		{1,1,1,1,1,1,1,1,1,1}
	};
	
	//Left turn
	private float[][] heightMap5 = {
		{1,2,3,4,5,6,7,8,9,10},
		{1,2,3,4,5,5,6,7,8,9},
		{1,2,3,4,5,5,6,6,7,8},
		{1,2,3,3,4,4,5,5,6,7},
		{1,2,2,3,3,4,4,5,5,6},
		{1,2,2,3,3,3,4,4,4,5},
		{1,2,2,2,3,3,3,3,4,4},
		{1,2,2,2,2,2,2,3,3,3},
		{1,1,2,2,2,2,2,2,2,2},
		{1,1,1,1,1,1,1,1,1,1}
	};
	
//	//Left turn
//	private float[][] heightMap5 = {
//		{1,1,1,1,1,1,1,1,1,1},
//		{2,2,2,2,2,2,2,2,1,1},
//		{3,3,3,2,2,2,2,2,2,1},
//		{4,4,3,3,3,3,2,2,2,1},
//		{5,4,4,4,3,3,3,2,2,1},
//		{6,5,5,4,4,3,3,2,2,1},
//		{7,6,5,5,4,4,3,3,2,1},
//		{8,7,6,6,5,5,4,3,2,1},
//		{9,8,7,6,5,5,4,3,2,1},
//		{10,9,8,7,6,5,4,3,2,1}
//	};

}
